import Base from './base.js';
import TestApi from '../api/test.js';
export default class extends Base {
    getDataByCallback() {
        // 我们无法解决响应问题，但是可以解决请求问题
        $.ajax({
            url: '/test/1',
            type: 'POST',
            success(data) {
                console.log(data);
                $.ajax({
                    url: '/test/2',
                    type: 'POST',
                    success(data) {
                        console.log(data);
                        $.ajax({
                            url: '/test/3',
                            type: 'POST',
                            success(data) {
                                console.log(data);
                                $.ajax({
                                    url: '/test/4',
                                    type: 'POST',
                                    success(data) {
                                        console.log(data);
                                        $.ajax({
                                            url: '/test/5',
                                            type: 'POST',
                                            success(data) {
                                                console.log(data);
                                            },
                                        });
                                    },
                                });
                            },
                        });
                    },
                });
            },
        });
    };
    getDataByPromise() {
        // resolve 异步成功之后，调用 resolve 方法，只有 resolve 被调用，then 才会被执行,失败的时候 reject 被调用catch 才会执行
        new Promise((resolve) => {
            $.ajax({
                url: '/test/1',
                type: 'POST',
                success: resolve
            });
        })
            // 成功后干嘛  
            .then((data) => {
                console.log(data);
                return new Promise((resolve) => {
                    $.ajax({
                        url: '/test/2',
                        type: 'POST',
                        success: resolve
                    });
                })
                    .then((data) => {
                        console.log(data);
                        return new Promise((resolve) => {
                            $.ajax({
                                url: '/test/3',
                                type: 'POST',
                                success: resolve
                            });
                        })
                            .then((data) => {
                                console.log(data);
                                return new Promise((resolve) => {
                                    $.ajax({
                                        url: '/test/4',
                                        type: 'POST',
                                        success: resolve
                                    });
                                })
                                    .then((data) => {
                                        console.log(data);
                                        return new Promise((resolve) => {
                                            $.ajax({
                                                url: '/test/5',
                                                type: 'POST',
                                                success: resolve
                                            });
                                        })
                                            .then((data) => {
                                                console.log(data);
                                            })
                                    })
                            })
                    })
            })
    }
    *getDataByGenerator() {
        let one = yield TestApi.getOne();
        let two = yield TestApi.getTwo();
        let three = yield TestApi.getThree();
        let four = yield TestApi.getFour();
        let five = yield TestApi.getFive();
        console.log([...one, ...two, ...three, ...four, ...five]);
    }
    async getDataByAsync() {
        let one = await TestApi.getOne();
        let two = await TestApi.getTwo();
        let three = await TestApi.getThree();
        let four = await TestApi.getFour();
        let five = await TestApi.getFive();
        console.log([...one, ...two, ...three, ...four, ...five]);
    }
    handle() {
        // 第一阶段
        this.getDataByCallbackBtn = $('.GET-DATA-CALLBACK');
        this.getDataByCallbackBtn.on('click', this.getDataByCallback.bind(this));
        // 第二阶段
        this.getDataByPromiseBtn = $('.GET-DATA-PROMISE');
        this.getDataByPromiseBtn.on('click', this.getDataByPromise.bind(this));
        // 第三阶段
        this.getDataByGeneratorBtn = $('.GET-DATA-GEN');
        this.getDataByGeneratorBtn.on('click', () => {
            run(this.getDataByGenerator.bind(this));
        })
        // 第四阶段
        this.getDataByAsyncBtn = $('.GET-DATA-ASYNC');
        this.getDataByAsyncBtn.on('click', this.getDataByAsync.bind(this));
    };
}
function run(gen) {
    // 拿到 迭代器
    const it = gen();
    // 递归调用，它必须有一个明确的终点
    function next(data) {
        const { value, done } = it.next(data);
        if (done) return;
        if (value instanceof Promise) {
            value.then((data) => next(data));
        } else {
            next(value);
        }
    }
    next();
}
// generator 生成器函数  函数声明时 在关键字 function + *
// function* fn() {
//     let a = yield 1;
//     console.log(a, 'a');
//     let b = yield 2;
//     console.log(b, 'b');
//     let c = yield 3;
//     console.log(c,'c');
//     return a + b + c
// }
// let it = fn();  // 当函数调用时并不会执行函数体内的代码    会返回一个 迭代器
// let {value,done}=it.next();   // 第一次调用执行到 yield 1 之前  
// it.next(value);   // 第二次调用执行到 yield 2 之前
// let data = it.next();  // 第三次调用执行到 yield 3 之前  
// console.log(data);   // it.next() 返回的是 {value: 2, done: false}   value 为 yield 后面的值，done 标志着函数是否执行完毕
// let a=it.next();
// let b=it.next(a.value);
// let c=it.next(b.value);
// let sum=it.next(c.value).value;
// console.log(sum);

